//
//  FNNewsDetailContView.m
//  FourNews
//
//  Created by xmg on 16/3/31.
//  Copyright © 2016年 天涯海北. All rights reserved.
//

#import "FNNewsDetailContView.h"
#import "FNNewsDetailFrame.h"
#import <UIImageView+WebCache.h>
#import "YLLabel.h"

@interface FNNewsDetailContView ()

@property (nonatomic, weak) UILabel *titleL;

@property (nonatomic, weak) UILabel *pTimeL;

@property (nonatomic, weak) UILabel *sourceL;

@property (nonatomic, weak) YLLabel *contentL;

@property (nonatomic, weak) UIView *shareV;

@property (nonatomic, weak) UIView *replyV;

@property (nonatomic, weak) UIView *relativeV;

@property (nonatomic, strong) NSMutableArray<UIImageView *> *imgVs;

@property (nonatomic, strong) NSMutableArray<UILabel *> *altLs;

@property (nonatomic, strong) NSMutableArray<NSValue *> *attrRanges;

@end

@implementation FNNewsDetailContView

- (NSMutableArray<UIImageView *> *)imgVs
{
    if (!_imgVs){
        self.imgVs = [[NSMutableArray alloc] init];
    }
    return _imgVs;
    
}

- (NSMutableArray<UILabel *> *)altLs
{
    if (!_altLs){
        self.altLs = [[NSMutableArray alloc] init];
    }
    return _altLs;
    
}

- (NSMutableArray<NSValue *> *)attrRanges
{
    if (!_attrRanges){
        self.attrRanges = [[NSMutableArray alloc] init];
    }
    return _attrRanges;
    
}


- (void)setAllSubviews
{
    UILabel *titleL = [[UILabel alloc] init];
    self.titleL = titleL;
    [self addSubview:titleL];
    
    UILabel *pTimeL = [[UILabel alloc] init];
    self.pTimeL = pTimeL;
    [self addSubview:pTimeL];

    UILabel *sourceL = [[UILabel alloc] init];
    self.sourceL = sourceL;
    [self addSubview:sourceL];
    
    YLLabel *contentL = [[YLLabel alloc] init];
    self.contentL = contentL;
    [self addSubview:contentL];
    
    self.contentL.font = [UIFont systemFontOfSize:17];
    self.contentL.textColor = FNColor(100, 100, 100);
    for (int i = 0; i < 100; i++) {
        if ([_detailItem.body containsString:@"<strong>"]) {
            NSRange rangeFront = [_detailItem.body rangeOfString:@"<strong>"];
            NSInteger locFront = rangeFront.location + 8;
            NSRange rangeBack = [_detailItem.body rangeOfString:@"</strong>"];
            NSInteger locBack = rangeBack.location;
            NSRange range = NSMakeRange(locFront, locBack-locFront);
            [self.attrRanges addObject:[NSValue valueWithRange:NSMakeRange(locFront-8, locBack-locFront)]];
            _detailItem.body = [_detailItem.body stringByReplacingCharactersInRange:rangeBack withString:@""];
            _detailItem.body = [_detailItem.body stringByReplacingCharactersInRange:rangeFront withString:@""];
        }
    }
    
    


    UIView *shareV = [[UIView alloc] init];
    [self addSubview:shareV];

    UIView *replyV = [[UIView alloc] init];
    [self addSubview:replyV];

    UIView *relativeV = [[UIView alloc] init];
    [self addSubview:relativeV];
    
    

}

- (void)setPicturesVWithItem:(FNNewsDetailItem *)item
{
    
}

- (void)setDetailItem:(FNNewsDetailItem *)detailItem
{
    
    _detailItem = detailItem;
    
    
    
    _detailItem.ptime = [_detailItem.ptime substringFromIndex:5];
    _detailItem.ptime = [_detailItem.ptime substringToIndex:11];
    
    _detailItem.body = [_detailItem.body stringByReplacingOccurrencesOfString:@"</p><p>" withString:@"\n\n"];
    _detailItem.body = [_detailItem.body stringByReplacingOccurrencesOfString:@"</p>" withString:@"\n"];
    _detailItem.body = [_detailItem.body stringByReplacingOccurrencesOfString:@"<p>" withString:@""];
    for (int i = 0; i<20; i++) {
        _detailItem.body = [_detailItem.body stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"<!--IMG#%d-->",i] withString:@""];
    }
    
    [self setAllSubviews];
    
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:_detailItem.body];
    for (int i = 0; i<self.attrRanges.count; i++) {
        [attr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:[self.attrRanges[i] rangeValue]];
        [attr addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:[self.attrRanges[i] rangeValue]];
    }
    self.contentL.string = attr;
    [self setDetailData:_detailItem];
    
    [self setDetailFrame:_detailItem];
}

- (void)setDetailData:(FNNewsDetailItem *)item
{
    self.titleL.text = item.title;
    self.pTimeL.text = item.ptime;
    self.sourceL.text = item.source;
//    self.contentL.text = item.body;
    [self setPictures];
    //    if (item.img.count != 0) {
//        [self.topImgV sd_setImageWithURL:[NSURL URLWithString:item.img[0][@"src"]]];
//    }
    
//    self.shareV
//    self.replyV
//    self.relativeV
}

- (void)setDetailFrame:(FNNewsDetailItem *)item
{
    FNNewsDetailFrame *contFrames = [[FNNewsDetailFrame alloc] init];
    
    contFrames.detailItem = item;
    
    self.titleL.frame = contFrames.titleF;
    self.titleL.font = [UIFont systemFontOfSize:20];
    
    self.pTimeL.frame = contFrames.pTimeF;
    self.pTimeL.font = [UIFont systemFontOfSize:12];
    self.pTimeL.textColor = FNColor(155, 155, 155);
    
    self.sourceL.frame = contFrames.sourceF;
    self.sourceL.font = [UIFont systemFontOfSize:12];
    self.sourceL.textColor = FNColor(155, 155, 155);
    
    [self setPictureFrames:contFrames];
//    self.topImgV.frame = contFrames.topImgF;
    
    self.contentL.frame = contFrames.contentF;
    
    
//    self.contentL.numberOfLines = 0;
//    self.contentL.lineBreakMode = NSLineBreakByCharWrapping;
    
    self.shareV.frame = contFrames.shareF;
    self.replyV.frame = contFrames.replyF;
    self.relativeV.frame = contFrames.relativeF;
}

- (void)setPictureFrames:(FNNewsDetailFrame *)frame
{
    for (int i = 0; i<_detailItem.img.count; i++) {
        CGRect imgF = [frame.pictureFs[i] CGRectValue];
        self.imgVs[i].frame = imgF;
        
        CGRect altF = [frame.altFs[i] CGRectValue];
        self.altLs[i].frame = altF;
        
    }
}

- (void)setPictures
{
    for (int i = 0; i<_detailItem.img.count; i++) {
        UIImageView *ImgV = [[UIImageView alloc] init];
        [self.imgVs addObject:ImgV];
        [ImgV sd_setImageWithURL:[NSURL URLWithString:_detailItem.img[i][@"src"]]];
        
        UILabel *altL = [[UILabel alloc] init];
        [self.altLs addObject:altL];
        [altL setText:_detailItem.img[i][@"alt"]];
        altL.textColor = FNColor(200, 200, 200);
        altL.font = [UIFont systemFontOfSize:14];
        altL.numberOfLines = 0;
        
        [self addSubview:ImgV];
        [self addSubview:altL];
    }
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    
}

@end
